<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="系统编号" prop="aiAgentPlatId">
        <el-input
          v-model="queryParams.aiAgentPlatId"
          placeholder="请输入系统编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台编号" prop="aiAgentBotId">
        <el-input
          v-model="queryParams.aiAgentBotId"
          placeholder="请输入平台编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker
          clearable
          v-model="queryParams.createdTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedTime">
        <el-date-picker
          clearable
          v-model="queryParams.updatedTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择更新时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['biz:aiAgent:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:aiAgent:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:aiAgent:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['biz:aiAgent:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="aiAgentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="aiAgentId" /> -->
      <!-- <el-table-column label="系统编号" align="center" prop="aiAgentPlatId" /> -->
      <el-table-column label="平台编号" align="center" prop="aiAgentBotId" />
      <el-table-column label="名称" align="center" prop="aiAgentName" />
      <!-- <el-table-column label="状态" align="center" prop="aiAgentSta" /> -->
      <el-table-column label="状态" align="center">
        <!-- 使用 scoped-slot 获取当前行数据 -->
        <template #default="scope">
          <!-- 根据 aiAgentSta 的值判断显示内容 -->
          <span v-if="scope.row.aiAgentSta === 1">正常</span>
          <span v-else>离线</span>
        </template>
      </el-table-column>
      <el-table-column label="图标" align="center">
        <template #default="scope">
          <img
            :src="scope.row.aiAgentIconUrl"
            alt="图标"
            style="width: 30px; height: 30px; object-fit: contain"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="图标" align="center" prop="aiAgentIconUrl" /> -->
      <el-table-column label="描述" align="center" prop="aiAgentDescription" />
      <!-- <el-table-column label="其他" align="center" prop="aiAgentOther" /> -->
      <!-- <el-table-column label="创建人" align="center" prop="createdBy" /> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="更新人" align="center" prop="updatedBy" /> -->
      <el-table-column
        label="更新时间"
        align="center"
        prop="updatedTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleGetCheckAiAgentSta(scope.row)"
            v-hasPermi="['biz:aiAgentPlat:edit']"
            >验证</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['biz:aiAgent:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['biz:aiAgent:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改智能体管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="aiAgentRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <!-- <el-form-item label="系统编号" prop="aiAgentPlatId">
          <el-input v-model="form.aiAgentPlatId" placeholder="请输入系统编号" />
        </el-form-item> -->

        <el-form-item label="服务平台">
          <el-select
            @change="handleAiAgentPlatChange"
            v-model="selectedAiAgentPlatId"
            placeholder="请选择服务平台"
          >
            <el-option
              v-for="item in aiAgentPlatList"
              :key="item.aiAgentPlatId"
              :label="item.aiAgentPlatName"
              :value="item.aiAgentPlatId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-button type="primary" @click="handleGetAiAgentBotList()"
          >获取智能体列表</el-button
        >

        <el-form-item label="智能体选择">
          <el-select
            @change="handleAiAgentBotIdChange"
            v-model="selectedAiAgentBotId"
            placeholder="请选择智能体"
          >
            <el-option
              v-for="item in aiAgentBotList"
              :key="item.aiAgentBotId"
              :label="item.aiAgentName"
              :value="item.aiAgentBotId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="智能体编号" prop="aiAgentBotId">
          <el-input
            v-model="form.aiAgentBotId"
            placeholder="请输入智能体编号"
          />
        </el-form-item>

        <!-- <el-form-item label="名称" prop="aiAgentName">
          <el-input v-model="form.aiAgentName" placeholder="请输入智能体名称" />
        </el-form-item> -->
        <el-form-item label="图标" prop="aiAgentIconUrl">
          <el-input
            v-model="form.aiAgentIconUrl"
            placeholder="请输入智能体图标"
          />
        </el-form-item>
        <el-form-item label="描述" prop="aiAgentDescription">
          <el-input
            v-model="form.aiAgentDescription"
            placeholder="请输入智能体描述"
          />
        </el-form-item>
        <!-- <el-form-item label="其他" prop="aiAgentOther">
          <el-input v-model="form.aiAgentOther" placeholder="请输入其他" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AiAgent">
import {
  listAiAgent,
  getAiAgent,
  delAiAgent,
  addAiAgent,
  updateAiAgent,
  getAiAgentBotList,
  getCheckAiAgentSta,
} from "@/api/biz/aiAgent";
import { listAiAgentPlat } from "@/api/biz/aiAgentPlat";

const { proxy } = getCurrentInstance();

const aiAgentList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
  selectedAiAgentBotId: null,
  selectedAiAgent: null,
  aiAgentBotList: [],
  selectedAiAgentPlatId: null,
  selectedAiAgentPlat: null,
  aiAgentPlatList: [],
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    aiAgentPlatId: null,
    aiAgentBotId: null,
    createdTime: null,
    updatedTime: null,
  },
  rules: {
    aiAgentPlatId: [
      {
        required: true,
        message: "智能体平台系统编号不能为空",
        trigger: "blur",
      },
    ],
    aiAgentBotId: [
      {
        required: true,
        message: "智能体平台智能体编号不能为空",
        trigger: "blur",
      },
    ],
  },
});

const {
  queryParams,
  form,
  rules,
  aiAgentPlatList,
  selectedAiAgentPlatId,
  selectedAiAgentPlat,
  aiAgentBotList,
  selectedAiAgentBotId,
  selectedAiAgent,
} = toRefs(data);

/** 查询智能体管理列表 */
function getAiAgentPlatList() {
  listAiAgentPlat(queryParams.value).then((response) => {
    aiAgentPlatList.value = response.rows;
  });
}
/** 查询智能体管理列表 */
function getList() {
  loading.value = true;
  listAiAgent(queryParams.value).then((response) => {
    aiAgentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    aiAgentId: null,
    aiAgentPlatId: null,
    aiAgentBotId: null,
    aiAgentName: null,
    aiAgentIconUrl: null,
    aiAgentDescription: null,
    aiAgentOther: null,
    revision: null,
    createdBy: null,
    createdTime: null,
    updatedBy: null,
    updatedTime: null,
    dataSta: null,
  };
  aiAgentBotList.value = [];
  proxy.resetForm("aiAgentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.aiAgentId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  getAiAgentPlatList();
  reset();
  open.value = true;
  title.value = "添加智能体管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _aiAgentId = row.aiAgentId || ids.value;
  getAiAgent(_aiAgentId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改智能体管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["aiAgentRef"].validate((valid) => {
    if (valid) {
      if (form.value.aiAgentId != null) {
        updateAiAgent(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addAiAgent(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _aiAgentIds = row.aiAgentId || ids.value;
  proxy.$modal
    .confirm('是否确认删除智能体管理编号为"' + _aiAgentIds + '"的数据项？')
    .then(function () {
      return delAiAgent(_aiAgentIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "biz/aiAgent/export",
    {
      ...queryParams.value,
    },
    `aiAgent_${new Date().getTime()}.xlsx`
  );
}

function handleAiAgentPlatChange(selectedAiAgentPlatId) {
  console.log("selectedAiAgentPlatId");
  console.log(selectedAiAgentPlatId);
  data.selectedAiAgentPlat = aiAgentPlatList.value.find(
    (item) => item.aiAgentPlatId === selectedAiAgentPlatId
  );

  console.log(data.selectedAiAgentPlat);
  if (!data.selectedAiAgentPlat) {
    return proxy.$modal.msgError("数据错误，请刷新页面！");
  }
  data.form.aiAgentPlatId = data.selectedAiAgentPlat.aiAgentPlatId;
}

function handleGetAiAgentBotList() {
  console.log("selectedAiAgentPlat");
  console.log(data.selectedAiAgentPlat);
  if (!data.selectedAiAgentPlat) {
    return proxy.$modal.msgError("请先选择智能体服务平台！");
  }

  getAiAgentBotList(data.selectedAiAgentPlat.aiAgentPlatId).then((response) => {
    console.log(response);
    console.log(response.data);
    console.log(aiAgentBotList);
    aiAgentBotList.value = response.data;
    console.log(aiAgentBotList);
  });
}

function handleAiAgentBotIdChange(selectedAiAgentBotId) {
  console.log("selectedAiAgentBotId");
  console.log(selectedAiAgentBotId);
  data.selectedAiAgent = aiAgentBotList.value.find(
    (item) => item.aiAgentBotId === selectedAiAgentBotId
  );
  data.form.aiAgentBotId = data.selectedAiAgent.aiAgentBotId;
  data.form.aiAgentName = data.selectedAiAgent.aiAgentName;
  data.form.aiAgentIconUrl = data.selectedAiAgent.aiAgentIconUrl;
  data.form.aiAgentDescription = data.selectedAiAgent.aiAgentDescription;
  // data.form.aiAgentBotId = data.selectedAiAgent.aiAgentBotId
  console.log(form);
}

/** 验证token是否有效操作 */
function handleGetCheckAiAgentSta(row) {
  console.log(row);
  const _aiAgentId = row.aiAgentId || ids.value;
  getCheckAiAgentSta(_aiAgentId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改智能体平台";
  });
}

getList();
</script>
